package com.synnex.mobile.servier.web.resource.controller.admin;

import com.synnex.mobile.generic.core.exception.SystemException;
import com.synnex.mobile.generic.core.util.MultiValueMap;
import com.synnex.mobile.generic.core.util.StringUtil;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
import com.synnex.mobile.generic.log.entity.LogEntry;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.generic.web.view.RedirectView;
import com.synnex.mobile.generic.web.view.View;
import com.synnex.mobile.servier.core.contact.bo.ContactBo;
import com.synnex.mobile.servier.core.resource.dto.Resource;
import com.synnex.mobile.servier.web.contact.controller.AbstractContactController;
import com.synnex.mobile.servier.web.contact.form.ContactForm;
import com.synnex.mobile.servier.web.resource.controller.AbstractResourceController;
import com.synnex.mobile.servier.web.resource.form.ResourceForm;
import com.synnex.mobile.servier.web.utils.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kerryf
 */
@RequestMapping(url = "/admin/resource")
@LogClass(name = "admin resource")
public class AdminResourceController extends AbstractResourceController {


    @RequestMapping(url = "/listResource.do")
    @LogMethod(name = "get activity record list ")
    public ModelAndView listResource(HttpServletRequest request, HttpServletResponse response) {
        try {

            String downLoadLink = "http://"+this.getConfig().getFtpHost()+this.getConfig().getFtpDownLoadPath()+"/";
            Map<String, Object> model = new HashMap<String, Object>();
            String key = request.getParameter("title");
            List<Resource> recordBos = resourceService.ListResource(key);
            model.put("results", recordBos);
            model.put("totalCount", recordBos.size());
            model.put("link", downLoadLink);

            return new ModelAndView(model);

        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @RequestMapping(url = "/addResource.do")
    @LogMethod(name="jump to add Resource page")
    public ModelAndView addResource(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String,Object> model = new HashMap<String,Object>();
            return new ModelAndView(model);

        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @RequestMapping(url = "/addResourceAction.do", formCommand = "addResource")
    @LogMethod(name="add a new Resource")
    public ModelAndView addResourceAction(HttpServletRequest request, HttpServletResponse response,ResourceForm form) {
        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        try {
            Resource bo = new Resource();
            String name = request.getParameter("name");
            String seq = request.getParameter("seq");

             bo.setName(name);

            if(seq==null || seq.equals("") || !StringUtil.isInteger(seq)){
                bo.setSeq(99);
            }else{
                bo.setSeq(Integer.parseInt(seq));
            }

            MultiValueMap url = uploadFilesToFtp(request, "fileName", null);
            if (null != url.get("fileName")) {
                String fileName = url.get("fileName").toString();

                bo.setIcon(fileName.substring(1, fileName.length() - 1));
            }

            resourceService.addResource(bo);

            Map<String,Object> model = new HashMap<String,Object>();

            log.setItemText("name: "+bo.getName());

            return new ModelAndView(new RedirectView("/admin/resource/listResource.do"),model);

        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @RequestMapping(url="/viewResource.do")
    @LogMethod(name="view Resource by id")
    public ModelAndView viewResource(HttpServletRequest request,HttpServletResponse response){

        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        String downLoadLink = "http://"+this.getConfig().getFtpHost()+this.getConfig().getFtpDownLoadPath()+"/";
        Map<String,Object> model = new HashMap<String, Object>();

        String id = request.getParameter("id");

        Resource resource = resourceService.getResourceById(Integer.parseInt(id)) ;
        model.put("data",resource);
        model.put("link",downLoadLink+resource.getIcon());

        log.setItemText(id);
        return new ModelAndView(model);
    }

    @RequestMapping(url="/editResource.do",formCommand = "viewResource")
    @LogMethod(name="edit contact")
    public ModelAndView editResource(HttpServletRequest request,HttpServletResponse response, ResourceForm form){

        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        Resource bo = new Resource();
        String name = request.getParameter("name");
        String seq = request.getParameter("seq");
        String entryDate = request.getParameter("entryDate") ;
        String icon = request.getParameter("icon");
        String id = request.getParameter("id");

        bo.setName(name);


        bo.setEntryDate(DateUtil.strToDate(entryDate));
        bo.setId(Integer.parseInt(id));

        if(seq==null || seq.equals("") || !StringUtil.isInteger(seq)){
            bo.setSeq(99);
        }else{
            bo.setSeq(Integer.parseInt(seq));
        }

        String fileName = null;
        if(icon!=null && !icon.equals("")){
            fileName = icon.substring(icon.lastIndexOf("/") + 1, icon.lastIndexOf("."));
        }
            MultiValueMap url = uploadFilesToFtp(request, "fileName", fileName);
            if (null != url.get("fileName")) {
                String iconName = url.get("fileName").toString();

                bo.setIcon(iconName.substring(1, iconName.length() - 1));
            } else{
                bo.setIcon(icon);
            }

        resourceService.editResource(bo);

        log.setItemText("name: "+bo.getName()+", company: ");
        return new ModelAndView(new RedirectView("/admin/resource/listResource.do"));
    }

    @RequestMapping(url="/deleteResource.do")
    @LogMethod(name="delete by id")
    public ModelAndView deleteResource(HttpServletRequest request,HttpServletResponse response){

        Map<String,Object> model = new HashMap<String,Object>();

        String id = request.getParameter("id");
        resourceService.deleteResourceById(Integer.parseInt(id));

        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        log.setItemText("delete Resource, id is "+ id);

        View view = new RedirectView("/admin/resource/listResource.do");
        return new ModelAndView(view,model);
    }




}
